package com.jushu.storbox.view.viewImpl;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.jushu.storbox.R;

/**
 * 
 * Slip Button
 * 
 */
public class SlipButton extends View implements OnTouchListener {
	private boolean NowChoose = false;

	private boolean isChecked;

	private boolean OnSlip = false;

	private float DownX, NowX;

	private Rect Btn_On, Btn_Off;

	private boolean isChgLsnOn = false;

	private OnChangedListener ChgLsn;

	private Bitmap bg_on, bg_off, slip_btn;

	/**
	 * SlipButton Controller function
	 * 
	 * @param context
	 *            Context
	 */
	public SlipButton(Context context) {
		super(context);
		initSlipButton();
	}

	/**
	 * SlipButton Controller function
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 */
	public SlipButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		initSlipButton();
	}

	/**
	 * SlipButton Controller function
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 * @param defStyle
	 *            int
	 */
	public SlipButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initSlipButton();
	}

	/**
	 * SlipButton initSlipButton
	 */
	private void initSlipButton() {

		bg_on = BitmapFactory.decodeResource(getResources(), R.mipmap.switch_on);
		bg_off = BitmapFactory.decodeResource(getResources(), R.mipmap.switch_off);
		slip_btn = BitmapFactory.decodeResource(getResources(), R.mipmap.switch_slip);
		Btn_On = new Rect(0, 0, slip_btn.getWidth(), slip_btn.getHeight());
		Btn_Off = new Rect(bg_off.getWidth() - slip_btn.getWidth(), 0, bg_off.getWidth(), slip_btn.getHeight());
		setOnTouchListener(this);
	}

	/**
	 * SlipButton initSlipButton
	 * 
	 * @param leftMap
	 *            Bitmap
	 * @param rightMap
	 *            Bitmap
	 * @param split
	 *            Bitmap
	 */
	public void initSlipButton(Bitmap leftMap, Bitmap rightMap, Bitmap split) {
		bg_on = leftMap;
		bg_off = rightMap;
		slip_btn = split;
		Btn_On = new Rect(0, 0, slip_btn.getWidth(), slip_btn.getHeight());
		Btn_Off = new Rect(bg_off.getWidth() - slip_btn.getWidth(), 0, bg_off.getWidth(), slip_btn.getHeight());
		setOnTouchListener(this);
	}

	/**
	 * SlipButton onDraw
	 * 
	 * @param canvas
	 *            Canvas
	 */
	@Override
	protected void onDraw(Canvas canvas) {

		super.onDraw(canvas);

		Matrix matrix = new Matrix();
		matrix.setTranslate(0, (slip_btn.getHeight() - bg_off.getHeight()) / 2);
		Paint paint = new Paint();
		float x;

		if (NowX < (bg_on.getWidth() / 2))
		{
			x = NowX - slip_btn.getWidth() / 2;
			canvas.drawBitmap(bg_off, matrix, paint);
		}

		else {
			x = bg_on.getWidth() - slip_btn.getWidth() / 2;
			canvas.drawBitmap(bg_on, matrix, paint);
		}

		if (OnSlip)

		{
			if (NowX >= bg_on.getWidth())

				x = bg_on.getWidth() - slip_btn.getWidth() / 2;

			else if (NowX < 0) {
				x = 0;
			} else {
				x = NowX - slip_btn.getWidth() / 2;
			}
		} else {

			if (NowChoose)
			{
				x = Btn_Off.left;
				canvas.drawBitmap(bg_on, matrix, paint);
			} else
				x = Btn_On.left;
		}
		if (isChecked) {
			canvas.drawBitmap(bg_on, matrix, paint);
			x = Btn_Off.left;
			isChecked = !isChecked;
		}

		if (x < 0)
			x = 0;
		else if (x > bg_on.getWidth() - slip_btn.getWidth())
			x = bg_on.getWidth() - slip_btn.getWidth();
		canvas.drawBitmap(slip_btn, x, 0, paint);

	}

	/**
	 * SlipButton onTouch
	 * 
	 * @param v
	 *            View
	 * @param event
	 *            MotionEvent
	 */
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction())

		{
		case MotionEvent.ACTION_MOVE:
			NowX = event.getX();
			break;

		case MotionEvent.ACTION_DOWN:

			if (event.getX() > bg_on.getWidth() || event.getY() > bg_on.getHeight())
				return false;
			OnSlip = true;
			DownX = event.getX();
			NowX = DownX;
			break;

		case MotionEvent.ACTION_CANCEL: 

			OnSlip = false;
			boolean choose = NowChoose;
			if (NowX >= (bg_on.getWidth() / 2)) {
				NowX = bg_on.getWidth() - slip_btn.getWidth() / 2;
				NowChoose = true;
			} else {
				NowX = NowX - slip_btn.getWidth() / 2;
				NowChoose = false;
			}
			if (isChgLsnOn && (choose != NowChoose)) 
				ChgLsn.OnChanged(NowChoose);
			break;
		case MotionEvent.ACTION_UP:

			OnSlip = false;
			boolean LastChoose = NowChoose;

			if (event.getX() >= (bg_on.getWidth() / 2)) {
				NowX = bg_on.getWidth() - slip_btn.getWidth() / 2;
				NowChoose = true;
			}

			else {
				NowX = NowX - slip_btn.getWidth() / 2;
				NowChoose = false;
			}

			if (isChgLsnOn && (LastChoose != NowChoose)) 

				ChgLsn.OnChanged(NowChoose);
			break;
		default:
		}
		invalidate();
		return true;
	}

	/**
	 * SlipButton SetOnChangedListener
	 * 
	 * @param l
	 *            OnChangedListener
	 */
	public void SetOnChangedListener(OnChangedListener l) {
		isChgLsnOn = true;
		ChgLsn = l;
	}

	/**
	 * SlipButton OnChangedListener interface
	 */
	public interface OnChangedListener {
		abstract void OnChanged(boolean CheckState);
	}

	/**
	 * SlipButton setCheck
	 * 
	 * @param isChecked
	 *            boolean
	 */
	public void setCheck(boolean isChecked) {
		this.isChecked = isChecked;
		NowChoose = isChecked;
		if(isChecked){
			NowX=bg_on.getWidth();
		}else{
			NowX=0;
		}
	}
	
	/**
	 * SlipButton onMeasure
	 * 
	 * @param widthMeasureSpec
	 *            int
	 * @param heightMeasureSpec
	 *            int
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(bg_on.getWidth(), slip_btn.getHeight());
	}
	
}
